import requests
import json
if __name__ == '__main__':
    url = "https://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname"
    #UA伪装
    headers={
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0"
    }
    while True:
        NAME=input("请输入你要查询的地址：")
        count=0 #统计一页中，是否是10条数据
        for i in range(1,11):
            data={
                'cname':NAME,
                'pid':'',
                'pageIndex':i,
                'pageSize':'10'
            }
            response = requests.post(url=url,data=data,headers=headers)
            dic_location = response.json()
            fp=open("./第二章：爬虫的实战/fp.肯德基餐厅查询.json",'a',encoding='utf-8')
            print("第",i,"页:",end=" ",file=fp)
            json.dump(dic_location,fp=fp,ensure_ascii=False,indent=4)
            print(file=fp)
        print("爬取结束！")
        
        
